function sum() {
    let result = 0;
    for (let i = 0; i < arguments.length; i++) {
        result += arguments[i]
    }
    return result
}
function asParam(num, callback) {
    return callback(num)
}
//重写map
function map(array, fn) {
    let result = []
    for (let i = 0; i < array.length; i++) {
        result.push(fn(array[i]))
    }
    return result;
}
//filter
function filter(array, fn) {
    let results = []
    for (let i = 0; i < array.length; i++) {
        if (fn(array[i])) {
            results.push(array[i])
        }
    }
    return results
}
//once
function once(fn) {
    let done = false;
    let num = 0
    let index = 0;
    return function () {
        index += 1;
        console.log(`付了${num}次,执行了${index}`)
        if (done) {
            return '已经执行过了'
        } else {
            fn.apply(this, arguments);
            num += 1
            done = true
        }
    }
}
function isExist() {
    let a = 0, b = 0;
    return function () {
        console.log('a=' + a, 'b=' + b)
        a++
        return undefined
    }
}

function memoize(f) {
    let cache = {}
    return function () {
        let arg_str = JSON.stringify(arguments)
        cache[arg_str] = cache[arg_str] || f.apply(f, arguments)
        return cache[arg_str]
    }
}
function getArea(r) {
    return Math.PI * r * r
}
let getAreaWith = memoize(getArea)
let pay = once(num => num + 1)
pay();
pay();
pay();
//手动实现 curry
function curry(fn) {
    return function curryFn(...args) {
        if (args.length < fn.length) {
            return function () {
                return curryFn(...args.concat(Array.from(arguments)))
            }

        }
        return fn(...args)
    }

}
function compose(Up, test, forArray) {
    return function (x) {
        return Up(test(forArray(x)))
    }
}
function forArray(value) {
    return value.split('')
}
function Up(values) {
    let arr = [];
    for (let i = 0; i < values.length; i++) {
        arr.push(values[i].toUpperCase())
    }
    return arr
}
function test(v) {
    console.log('aa', v)
    return v
}
let valueToArr = compose(Up, test, forArray)
class Container {
    static of(value) {
        return new Container(value)
    }
    static a=20
    constructor(value) {
        this._value = value
        this.aa='9999'
    }
    map(fn) {
        return Container.of(fn(this._value))
    }
}
let contain = new Container()

console.log(Container.map,contain.map)
module.exports = {
    sum,
    asParam,
    map,
    filter
}